package com.baozhun.mall.common.util

import com.orhanobut.logger.Logger
import java.util.regex.Matcher
import java.util.regex.Pattern


/**
 * @Title：
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2023/12/7
 * @Version：1.0.0
 */
object StringUtils {

    /**
     * 获取字符串中是否包含11位的手机号码
     */
    fun getPhoneNumber(targetStr: String): String? {
        val temp = targetStr.toCharArray()
        var value: String? = ""
        var licz = 0
        for (i in temp.indices) {
            if (licz < 11) {
                if (temp[i].toString().matches(Regex("[0-9]"))) {
                    value += temp[i].toString()
                    licz++
                } else if (temp[i].toString().matches(Regex("\u0020|\\-|\\(|\\)"))) {
                } else {
                    value = ""
                    licz = 0
                }
            }
        }
        value = if (value!!.length != 11) {
            null
        } else {
            value.trim { it <= ' ' }
        }
        Logger.e("手机号码=$value")
        return value
    }

    /**
     * 获取字符串里第一个数字
     */
    fun getFirstNumber(originString: String): String {
        // 定义正则表达式模式
        val pattern: Pattern = Pattern.compile("\\d+\\.?\\d*")
        val matcher: Matcher = pattern.matcher(originString)
        var matcherStr = ""
        while (matcher.find()) {
            matcherStr = matcher.group()
            break
        }
        return matcherStr
    }
}